<template>
  <div class="comTopWrap">
    <div class="selectWrap">
      <div
        class="selectItem"
        v-for="(group, i) in types"
        :key="group.title"
        tabindex="2"
        @click="onClickItem(group, i)"
        :style="{ color: group.dropdown ? '#b56cff' : '#333' }"
      >
        {{ group.title
        }}<span
          ><img
            :src="
              group.dropdown
                ? '/images/Duration_drop@2x.png'
                : '/images/Duration_drop-down@2x.png'
            "
        /></span>
      </div>
    </div>

    <div
      class="popdown"
      v-for="group in types"
      :key="group.title"
      v-show="group.dropdown"
    >
      <button
        v-for="(item, index) in group.tpls"
        :key="index"
        :class="{ active: item == group.curTp }"
        @click="btnClick(group, item)"
      >
        {{ item || group.curTitle }}
      </button>
    </div>
  </div>
  <div
    class="mode"
    v-show="showMod"
    @click="(types[itemIndex].dropdown = false), (showMod = false)"
  ></div>
</template>
<script>
export default {
  props: ["types"],
  emits: ["selectRes"],
  data() {
    return {
      showMod: false,
      itemIndex: 0,
      paramsObj: {},
    };
  },
  methods: {
    onClickItem(group, i) {
      group.dropdown = !group.dropdown;
      this.showMod = group.dropdown;
      if (this.itemIndex != i) {
        this.types[this.itemIndex].dropdown = false;
      }
      this.itemIndex = i;
    },
    btnClick(group, item) {
      group.curTp = item;
      this.paramsObj[group.paramsName] = item;
      this.$emit("selectRes", this.paramsObj);
      this.showMod = false;
      group.dropdown = false;
      console.log(group.title);
      group.title = item;
      group.title = group.title == "" ? group.curTitle : group.title;
    },
  },
};
</script>
<style lang="less" scoped>
.comTopWrap {
  width: 100%;
  position: relative;
  z-index: 100;
  background: #fff;
}

.selectWrap {
  display: flex;
  align-items: center;
  width: 100%;
  height: 0.3rem;
  justify-content: space-around;
  .selectItem {
    height: 0.11rem;
    line-height: 0.11rem;
    font-size: 0.11rem;
    span {
      display: inline-block;
      width: 0.1rem;
      height: 0.1rem;
      margin-left: 0.04rem;
      img {
        width: 100%;
      }
    }
  }
}
.popdown {
  position: absolute;
  width: 100%;
  background-color: #fff;
  left: 0;
  top: 0.3rem;
  z-index: 99;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: space-between;
  padding: 0.22rem 0.2rem;
  border-radius: 0 0 0.1rem 0.1rem;
  box-shadow: rgba(0, 0, 0, 0.07) 0px 1px 1px, rgba(0, 0, 0, 0.07) 0px 2px 2px,
    rgba(0, 0, 0, 0.07) 0px 4px 4px, rgba(0, 0, 0, 0.07) 0px 8px 8px,
    rgba(0, 0, 0, 0.07) 0px 6px 8px;
  border-top: 1px solid #ddd;
  button {
    border: none;
    outline: none;
    width: 0.79rem;
    height: 0.3rem;
    border-radius: 0.3rem;
    color: #333;
    font-size: 0.13rem;
    background-color: #ddd;
    margin-right: 0.063rem;
    margin-bottom: 0.1rem;
  }
  .active {
    background-color: #b56cff;
  }
}
.popdown button:nth-of-type(4n + 4) {
  margin-right: 0;
}
.mode {
  width: 100%;
  top: 0.9rem;
  background-color: rgba(0, 0, 0, 0.4);
  position: absolute;
  z-index: 90;
  bottom: 0.7rem;
  margin: auto;
  left: 0;
  right: 0;
}
</style>
